//
//  ScoresList.m
//  ToucheTouche
//
//  Created by polytech polytech on 12/01/12.
//  Copyright (c) 2012 polytech. All rights reserved.
//

#import "ScoresList.h"

@implementation ScoresList

@synthesize stringScoresListWorld;
@synthesize stringScoresListLocal;
@synthesize stringRank;
@synthesize score;
@synthesize datetime;

static ScoresList *sharedScoresList = nil;

-(id)init {
    if (self = [super init]) {
        stringPathScoreLocal = [[NSString alloc] initWithString:@"scores"];        
        stringPathScoreWorld = [[NSString alloc] initWithString:@"http://www.sacre-willy.com/touche-touche/"];
        
        stringScoresListLocal = [[NSString alloc] init];
        stringScoresListWorld = [[NSString alloc] init];
        
        score = [[NSNumber alloc] initWithInt:0];
        datetime = [[NSDate alloc] init];
        stringRank = [[NSString alloc] initWithString:@"toto-/-"];
        [self loadScoreLocal];
    }
    return self;
}

-(void)dealloc {
    [stringScoresListWorld release];
    [stringScoresListLocal release];
    [stringPathScoreWorld release];
    [stringScoresListLocal release];
    [stringRank release];
    [score release];
    [datetime release];
    [super dealloc ];
}


-(void)loadScoreLocal {
     NSString *stringUrl = [[NSString stringWithFormat:@"%@?action=iphone&phone_id=%@",
     stringPathScoreWorld,
     [[UIDevice currentDevice] uniqueIdentifier]]stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
     
     
     NSLog(@"add stringUrla : %@", stringUrl);
     
     NSURL *pathScoreLocal = [NSURL URLWithString:stringUrl];
     NSURLRequest *request = [NSURLRequest requestWithURL:pathScoreLocal];
     NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
     stringScoresListLocal = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    NSError *error;
    NSArray *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [rootPath objectAtIndex:0];
    plistPath = [documentsDirectory stringByAppendingPathComponent:[stringPathScoreLocal stringByAppendingString:@"plist"]];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if (![fileManager fileExistsAtPath:plistPath]) {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:stringPathScoreLocal ofType:@"plist"];
        //[fileManager copyItemAtPath:bundle toPath:plistPath error:&error];
        NSLog(@"Création de la plist des scores");
    }
}

-(void)loadScoreWorld {
    
    NSURL *pathScoreWorld = [NSURL URLWithString:[NSString stringWithFormat:@"%@?action=iphone", stringPathScoreWorld]];
    NSURLRequest *request = [NSURLRequest requestWithURL:pathScoreWorld];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    stringScoresListWorld = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    stringRank = [NSString stringWithString:@"12/109"];
}

////////////////////
// Singleton Methods
+ (ScoresList *)sharedScoresList
{
    if (sharedScoresList == nil) {
        sharedScoresList = [[super allocWithZone:NULL] init];
    }
    return sharedScoresList;
}

+ (id)allocWithZone:(NSZone *)zone {
    return [[self sharedScoresList] retain];
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (NSUInteger)retainCount {
    return NSUIntegerMax;  //denotes an object that cannot be released
}
/*
- (void)release {
    //do nothing
}
*/

- (id)autorelease {
    return self;
}

////////////////////
// Shared Methods
- (NSString *)getStringScoresListWorld {
    return stringScoresListWorld;  
}

- (NSString *)getStringScoresListLocal {
    return stringScoresListLocal; 
}

- (NSString *)getRank {
    return stringRank; 
}
- (NSNumber *)getScore {
    return score; 
}


- (void)setScore:(NSNumber *)newScore datetime:(NSDate *)newDatetime {
    score = newScore;
    datetime = newDatetime;
}


- (BOOL)addNickname:(NSString *)nickname {    
    NSString *stringUrl = [[NSString stringWithFormat:@"%@?action=add&score=%@&nickname=%@&phone_id=%@&version=%@&os=%@&model=%@&device_name=%@",
                          stringPathScoreWorld,
                          score,
                          nickname,
                          [[UIDevice currentDevice] uniqueIdentifier],
                          [[UIDevice currentDevice] systemVersion],
                          [[UIDevice currentDevice] systemName],
                          [[UIDevice currentDevice] model],
                          [[UIDevice currentDevice] name]]stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    
    
    NSLog(@"add stringUrla : %@", stringUrl);
    
    NSURL *pathScoreAdd = [NSURL URLWithString:stringUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:pathScoreAdd];
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    /*
    
    // Ajout nouvelle valeur
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    
    [data setValue:[NSString stringWithFormat:@"eee"] forKey:@"valeur1"];
    [data setValue:[NSString stringWithFormat:@"ddd"] forKey:@"valeur2"];       
    NSLog(@"newData : %@", data);
    
    NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    
    NSLog(@"ancien plistDict : %@", plistDict);
    
    NSMutableArray *specifiers = [plistDict objectForKey:@"Root"];
    
    NSLog(@"specifiers : %@", specifiers);
    
    
    if (plistDict) {           
        [specifiers addObject:data];
        NSLog(@"Ajout plist : %@", plistDict);
    }
    
    [plistDict writeToFile:plistPath atomically:YES];
     */
    return YES;
}
@end